layui.use(['table','layer','form'],function(){
	let table = layui.table;
	let add_layer = layui.layer;
	let form = layui.form;
	// 获取数据列表
	function getItemList(){
		table.render({
			elem:'#dataTable',
			height: 500,
			url: 'http://chonghekj.com/crud/index.php/Home/Index/getItemList',
			page:true,
			toolbar:'default',
			request: {
				pageName:'pageNumber',
				limitName:'pageSize'
			},
			limits:[5,10,15],
			cols:[[
				{type:'checkbox'},
				{field:'item_id',title:'ID',width:100},
				{field:'item_name',title:'物品名称',width:120},
				{field:'item_price',title:'单价',width:120},
				{field:'item_format',title:'单位',width:120},
				{field:'item_total',title:'数量',width:120},
				{field:'item_state',title:'库存状态',width:120},
				{field:'item_inventory',title:'库存预警',width:120},
				{field:'create_time',title:'创建时间',width:120},
				{field:'update_time',title:'修改时间',width:120},
				{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
			]],
			parseData:function(res){
				return {
					"code":0,
					"msg":'',
					"count":res.total,
					"data":res.rows,
				}
			},
			
		});
	}
	getItemList();
	// 添加数据
	table.on('toolbar(dataTable)',function(obj){
		switch(obj.event){
			case 'add':
				add_layer.open({
					type:1,
					content:$('.add_form'),
					area:['600px','600px'],
				})
		}
		form.on('submit(formDemo)',function(){
			item_name = $('.add_item_name').val();
			item_price = $('.add_item_price').val();
			item_format = $('.add_item_format').val();
			item_total = $('.add_item_total').val();
			item_inventory = $('.add_item_inventory').val();
			$.ajax({
				url:'http://chonghekj.com/crud/index.php/Home/Index/addItem',
				type: 'post',
				dataType: 'json',
				data:{
					item_name,
					item_price,
					item_format,
					item_total,
					item_inventory
				},
				success: function(res) {
					console.log(res);
					getItemList();
				},
			})
		})
	})
	// 修改
	let checkbox = [];
	let ids = [];
	table.on('checkbox(dataTable)',function(obj){
		 if(obj.checked){
			checkbox.push(obj.data)
			ids.push(obj.data.item_id)
		 }else{
			for(var i = 0; i<checkbox.length; i++){
				if(obj.data.item_id == ids[i]){
					checkbox.splice(i ,1);
					ids.splice(i,1)
				}
			}
		 }
		 console.log(checkbox)
		 console.log(ids)
		table.on('toolbar(dataTable)',function(obj){
			
		})
		 
		 
		 
		 
	})
})